using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Collections.Generic;

public partial class CentreStageEntry : BaseTemplate
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bool redirect = true;
            int postId = 0;
            try
            {
                postId = int.Parse(Request.QueryString["PostId"]);
            }
            catch
            {
            }

            if (Request.IsAuthenticated && postId > 0)
            {
                PostId = postId;
                Acms.Bll.Post post = Acms.Bll.Post.GetById(AcmsContext.SiteId, postId, AcmsContext.SiteVersion);
                if (post != null)
                {
                    redirect = false;
                    //if already an entry for the user then redirect
                    List<Acms.Bll.UserPostUpload> uploads = Acms.Bll.UserPostUpload.GetAllByUserIdAndPostId(User.Identity.User.Id, post.Id);
                    if (uploads.Count > 0)
                    {
                        Response.Redirect(Page.ResolveUrl(post.Url));
                    }

                    //set cancel
                    btnCancel.OnClientClick = string.Format("javascript:window.location = \"{0}\";", Page.ResolveUrl(post.Url));
                    ReturnUrl = post.Url;
                    //find header
                    Acms.Bll.PostControl tmpFlashHeader = post.FindPostControl("FlashHeader");
                    if (tmpFlashHeader != null)
                    {
                        Acms.AcmsControls.FlashContentControlProperties tmpFlashHeaderProps = tmpFlashHeader.GetProperties<Acms.AcmsControls.FlashContentControlProperties>();
                        if (tmpFlashHeaderProps != null && tmpFlashHeaderProps.ResourceId > 0)
                        {
                            flashHeader.FlashFileUrl = string.Concat("~/File.aspx?Id=", tmpFlashHeaderProps.ResourceId);
                            flashHeader.Width = tmpFlashHeaderProps.Width;
                            flashHeader.Height = tmpFlashHeaderProps.Height;
                            flashHeader.Variables.Add("SiteRoot", this.AcmsContext.SiteRoot);
                        }
                    }

                    //find flashIntro
                    Acms.Bll.PostControl tmpFlashIntro = post.FindPostControl("FlashIntro");
                    if (tmpFlashIntro != null)
                    {
                        Acms.AcmsControls.FlashContentControlProperties tmpFlashIntroProps = tmpFlashIntro.GetProperties<Acms.AcmsControls.FlashContentControlProperties>();
                        if (tmpFlashIntroProps != null && tmpFlashIntroProps.ResourceId > 0)
                        {
                            flashIntro.Visible = true;
                            flashIntro.FlashFileUrl = string.Concat("~/File.aspx?Id=", tmpFlashIntroProps.ResourceId);
                            flashIntro.Width = tmpFlashIntroProps.Width;
                            flashIntro.Height = tmpFlashIntroProps.Height;
                        }
                    }

                    //find imageIntro
                    Acms.Bll.PostControl tmpImageInto = post.FindPostControl("ImageIntro");
                    if (tmpImageInto != null)
                    {
                        Acms.AcmsControls.ImageContentControlProperties tmpImageIntoProps = tmpImageInto.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                        if (tmpImageIntoProps != null && tmpImageIntoProps.ResourceId > 0)
                        {
                            imageIntro.FileId = tmpImageIntoProps.ResourceId;
                            imageIntro.AlternateText = tmpImageIntoProps.AltText;
                        }
                    }
                }
            }
            if (redirect)
            {
                Response.Redirect(Page.ResolveUrl("~"));
            }
        }

    }

    protected int PostId
    {
        get 
        {
            if (ViewState["PostId"] is int)
            {
                return (int)ViewState["PostId"];
            }
            else
            {
                return 0;
            }
        }
        set 
        {
            ViewState["PostId"] = value;
        }
    }
	

    public string ReturnUrl
    {
        get { return ViewState["ReturnUrl"] as string; }
        set { ViewState["ReturnUrl"] = value; }
    }
	
    protected void btnEnter_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && RadUploadContext.Current.UploadedFiles.Count == 2)
        {
            UploadedFile image = RadUploadContext.Current.UploadedFiles[0];
            UploadedFile mp3 = RadUploadContext.Current.UploadedFiles[1];

            if (image.ContentLength > 2000000 || mp3.ContentLength > 6000000)
            {
                divError.Visible = true;
            }
            else
            {
                Acms.Bll.UserPostUpload entry = Acms.Bll.UserPostUpload.CreateInstance(User.Identity.User.Id, PostId, System.Net.IPAddress.Parse(Request.ServerVariables["REMOTE_ADDR"]));
                entry.Title = txtTitle.Text;
                entry.Comment = txtBio.Text;
                entry.SetTextValue("TrackTitle", txtTrackTitle.Text);
                entry.SetFile("Track", Acms.Bll.File.FileTypes.Mp3, mp3.GetNameWithoutExtension(), mp3.ContentType, mp3.ContentLength, mp3.InputStream);
                try
                {
                    entry.Update(Acms.Bll.File.FileTypes.Image, image.GetNameWithoutExtension(), image.ContentType, image.ContentLength, image.InputStream);
                }
                catch
                {
                    divError.Visible = true;
                    return;
                }

                //Send Email
                Acms.Bll.Email email = Acms.Bll.Email.CreateInstance(User.Identity.User, MapPath("~/Emails/Mp3Competition.xml"));
                email.Format("EntryDate", DateTime.Now.ToLongDateString());
                email.Format("CompetitionName", AcmsContext.CurrentPost.DisplayName);
                email.Send();

                Response.Redirect(ResolveUrl(ReturnUrl));
            }

            Acms.Bll.File file = Acms.Bll.File.CreateInstance(Acms.Bll.File.FileTypes.Image, image.GetNameWithoutExtension(), image.ContentType, image.ContentLength, image.InputStream);
            file.Update();
        }
        else
        {
            divError.Visible = true;
        }
    }
}
